#include <gtkmm.h>

class MyWindow : public Gtk::Window {
public:
    MyWindow() {
        set_title("GtkToolBar Example");  // 设置窗口标题
        set_default_size(400, 200);  // 设置窗口默认大小

        // 创建主垂直盒子容器
        Gtk::Box* vbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0));
        add(*vbox);

        // 创建工具栏
        Gtk::Toolbar* toolbar = Gtk::manage(new Gtk::Toolbar());
        vbox->pack_start(*toolbar, Gtk::PACK_SHRINK);

        // 创建工具按钮
        Gtk::ToolButton* new_button = Gtk::manage(new Gtk::ToolButton(Gtk::Stock::NEW));
        new_button->signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_new_button_clicked));
        toolbar->append(*new_button);

        Gtk::ToolButton* open_button = Gtk::manage(new Gtk::ToolButton(Gtk::Stock::OPEN));
        open_button->signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_open_button_clicked));
        toolbar->append(*open_button);

        Gtk::ToolButton* save_button = Gtk::manage(new Gtk::ToolButton(Gtk::Stock::SAVE));
        save_button->signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_save_button_clicked));
        toolbar->append(*save_button);

        // 创建分隔符
        Gtk::SeparatorToolItem* sep = Gtk::manage(new Gtk::SeparatorToolItem());
        toolbar->append(*sep);

        // 创建退出按钮
        Gtk::ToolButton* quit_button = Gtk::manage(new Gtk::ToolButton(Gtk::Stock::QUIT));
        quit_button->signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_quit_button_clicked));
        toolbar->append(*quit_button);

        // 创建标签
        label = Gtk::manage(new Gtk::Label("Toolbar example"));
        vbox->pack_start(*label);

        show_all_children();
    }

    virtual ~MyWindow() {}

protected:
    // 工具按钮的回调函数
    void on_new_button_clicked() {
        label->set_text("New button clicked");
    }

    void on_open_button_clicked() {
        label->set_text("Open button clicked");
    }

    void on_save_button_clicked() {
        label->set_text("Save button clicked");
    }

    void on_quit_button_clicked() {
        hide();  // 退出程序
    }

    Gtk::Label* label;  // 标签控件指针
};

int main(int argc, char *argv[]) {
    auto app = Gtk::Application::create(argc, argv, "com.example.GtkToolBarExample");  // 创建 GTK 应用程序实例

    MyWindow window;  // 创建主窗口实例

    return app->run(window);  // 运行应用程序
}

